package cn.bdqn.controller;


import cn.bdqn.dao.Ebook_entryMapper;
import cn.bdqn.pojo.Ebook_category;
import cn.bdqn.pojo.Ebook_entry;
import cn.bdqn.service.Ebook_categoryService;
import cn.bdqn.service.Ebook_entryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/book")
public class bookController {

    @Autowired
    @Qualifier("ebook_categoryService")
private Ebook_categoryService ebook_categoryService;

    @Autowired
    @Qualifier("ebook_entryService")
    private Ebook_entryService ebook_entryService;



@RequestMapping(value =  "/bookList.do")
public String bookList(Integer categoryId, Integer pageIndex, Model model){
    List<Ebook_category> list = ebook_categoryService.getAll();
    model.addAttribute("list",list);

    if (StringUtils.isEmpty(categoryId)){
        categoryId =0;
    }
    if (StringUtils.isEmpty(pageIndex)){
        pageIndex=1;
    }
    List<Ebook_entry> entryList = ebook_entryService.getUserList(categoryId, pageIndex);
    model.addAttribute("entryList",entryList);

    int count=ebook_entryService.getcount(categoryId);
    model.addAttribute("count",count);

   int pages=ebook_entryService.getPages(categoryId);
    model.addAttribute("pages",pages);

    model.addAttribute("pageIndex",pageIndex);
    return "bookList";
}

@RequestMapping(value = "bookadd")
public String bookadd(Model model){
    List<Ebook_category> list = ebook_categoryService.getAll();
    model.addAttribute("addList",list);
    return "bookadd";
}

@RequestMapping(value = "/add")
    public String add(Model model,Ebook_entry ebook_entry){
    int count = ebook_entryService.addEntry(ebook_entry);
        if (count>0){
            return "redirect:/book/bookList.do";
        }else {
         return "bookadd";
        }
}

@RequestMapping(value = "/modify/{id}",method = RequestMethod.GET)
    public String modify(@PathVariable("id") Integer id,Model model){
    List<Ebook_category> list = ebook_categoryService.getAll();
    model.addAttribute("list",list);

    Ebook_entry entryById = ebook_entryService.getEntryById(id);
    model.addAttribute("entryById",entryById);
    return "bookupdate";
}

@RequestMapping(value = "/bookupdate",method = RequestMethod.POST)
    public String update(Model model,Ebook_entry ebook_entry){
    int update = ebook_entryService.update(ebook_entry);
    return "redirect:/book/bookList.do";
}

@RequestMapping(value = "bookdel")
    public String bookdel(Integer id){
    int del = ebook_entryService.del(id);
    return "redirect:/book/bookList.do";
}
}
